const model = require("../model/model.js")
const goodsSupervise={};
goodsSupervise.select=async (req,res)=>{
    let {intName} = req.query
    let where = '';
    if(intName){
     where+= ` where goodsName like '%${intName}%'`
    }
    let sql = `SELECT * FROM shopgoods ${where} `
    result=await model(sql)
    // console.log(result);
    res.json(result)
}
goodsSupervise.addgoods=async (req,res)=>{
    console.log(req.body);
    let { goodsName,goodsprice,goods_num,goods_img="",is_online=0}=req.body
    console.log(goodsName,goodsprice,goods_num,goods_img,is_online);
   let  goods_addtime=gettime();
   console.log(goods_addtime);
   function gettime(){
       return nowtime=parseInt( Date.now() / 1000);
   };
    let sql = `INSERT INTO shopGoods(goodsName,goodsprice,goods_addtime,goods_num,goods_img,is_online)  VALUES("${goodsName}",${goodsprice},${goods_addtime},${goods_num},"${goods_img}",${is_online})`
    console.log(sql)
     result=await model(sql);
     res.json({status : 20000})
};
goodsSupervise.editGoods=async (req,res)=>{
    console.log(req.body);
    let { goods_id,goodsName,goodsprice,goods_num,goods_img,is_online}=req.body;
    let sql=`UPDATE shopgoods SET goodsName="${goodsName}",goodsprice="${goodsprice}",
    goods_num=${goods_num},goods_img="${goods_img}",is_online=${is_online} where goods_id=${goods_id}`;
    result=await model(sql);
    res.json({status : 20000})

};
goodsSupervise.deleGoods=async (req,res)=>{
    let {goods_id} =req.query;
    console.log(goods_id);
    let sql = `delete from shopgoods where goods_id=${goods_id}`
    result=await model(sql);
    res.json({status : 20000})
}

module.exports=goodsSupervise;